package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.MyBaseAdapter;
import com.zzh.takeoutapp.model.AddressBean;
import com.zzh.takeoutapp.utils.DensityUtil;
import com.zzh.takeoutapp.view.SlidingButtonView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/31.
 */

public class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
        SlidingButtonView.IonSlidingButtonListener {
    private Context mContext;
    private List<AddressBean> list;
    private SlidingButtonView mMenu = null;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;

    public IonSlidingViewClickListener getmIDeleteBtnClickListener() {
        return mIDeleteBtnClickListener;
    }

    public void setmIDeleteBtnClickListener(IonSlidingViewClickListener mIDeleteBtnClickListener) {
        this.mIDeleteBtnClickListener = mIDeleteBtnClickListener;
    }

    public AddressAdapter(List<AddressBean> list, Context context) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup container, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_mine_address, container, false);
        return new ViewOneHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewOneHolder) {
            AddressBean addressBean = list.get(position);
            ((ViewOneHolder) holder).address_name.setText(addressBean.getAddr());
            ((ViewOneHolder) holder).tv_user_name.setText(addressBean.getContact());
            ((ViewOneHolder) holder).tv_phone.setText(String.valueOf(addressBean.getMobile()));
            ((ViewOneHolder) holder).layout_content.getLayoutParams().width = DensityUtil.getScreenWidth(mContext);
            //((ViewOneHolder) holder).itemView.setBackgroundResource(R.drawable.item_background);
            ((ViewOneHolder) holder).textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (menuIsOpen()) {
                        closeMenu();
                    } else {
                        int n = holder.getLayoutPosition();
                        mIDeleteBtnClickListener.onItemClick(v, n);
                    }

                }
            });

            ((ViewOneHolder) holder).tv_remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mIDeleteBtnClickListener.onDeleteBtnCilck(v, position);
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mIDeleteBtnClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnCilck(View view, int position);
    }

    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }


    //普通的Holder
    public class ViewOneHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_remove)
        TextView tv_remove;
        @Bind(R.id.address_name)
        TextView address_name;
        @Bind(R.id.tv_user_name)
        TextView tv_user_name;
        @Bind(R.id.tv_phone)
        TextView tv_phone;
        @Bind(R.id.iv_alter)
        ImageView iv_alter;
        @Bind(R.id.text)
        TextView textView;
        @Bind(R.id.layout_content)
        ViewGroup layout_content;
        @Bind(R.id.sliding_btn_view)
        SlidingButtonView sliding_btn_view;
        public ViewOneHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ((SlidingButtonView) itemView).setSlidingButtonListener(AddressAdapter.this);
        }
    }
}
